/*
#include <Arduino.h>
#include <Wire.h>
// #include <VL53L1X.h>

// HardwareSerial Serial1(PB7, PB6);

void setup()
{
  pinMode(PB14,OUTPUT);
  // Serial1.begin(115200);
  // Serial1.println(F("SSD1306 allocation OK"));
}

int main()
{
  // Serial1.println(F("SSD1306 allocation OK"));
  digitalWrite(PB14, HIGH);
  delay(500);
  digitalWrite(PB14, LOW);
  delay(500);
}

*/

/*
eXoCanDataSim.ino                                                            4/30/20

Simulates varying data from multiple sensors and sends data to the CAN and Serial busses.

****** REMEMBER No Serial USB with CAN *********

TJA1050 boards work, but must have a good +5V.  USB to UART adapters don't supply
enough current, +5V is down to 3V at blue pill.  USB from PC works.                   1/7

just sending 8 bytes in a tight loop = 488uS msg to msg at br250K                     1/14
444uS msg, 44uS dead time

RAM:   [=         ]   5.4% (used 1108 bytes from 20480 bytes)
Flash: [==        ]  18.1% (used 23692 bytes from 131072 bytes)  working

*/

/*

#include <arduino.h>
#include <eXoCAN.h>

#define PRINT true
#define VERS "eXoCanDataRx"

MSG rxMsg; //
int id;
eXoCAN can(STD_ID_LEN, BR500K, PORTB_8_9_WIRE_PULLUP); // constuctor for eXoCAN lib

char cBuff[80];                          // holds formatted string
void canTX(msgFrm frm, bool prt = false) // send a CAN Bus message
{
  uint8_t msgLen = frm.txMsgLen;
  can.transmit(frm.txMsgID, frm.txMsg.bytes, msgLen);
  if (prt)
  {
    sprintf(cBuff, "tx @%02x #%d \t", frm.txMsgID, msgLen);
    Serial.print(cBuff);
    for (int j = 0; j < msgLen; j++)
    {
      sprintf(cBuff, "%02x ", frm.txMsg.bytes[j]);
      Serial.print(cBuff);
    }
    Serial.println();
  }
}

int canRX(bool print = false) // check for a CAN Bus message
{
  int len, id = 0, fltIdx;
  len = can.receive(id, fltIdx, rxMsg.bytes);
  if (print)
  {
    if (len >= 0)
    {
      sprintf(cBuff, "RX @%02x #%d  %d\t", id, len, fltIdx);
      Serial.print(cBuff);
      for (int j = 0; j < len; j++)
      {
        sprintf(cBuff, "%02x ", rxMsg.bytes[j]);
        Serial.print(cBuff);
      }
      Serial.println();
    }
  }
  return id;
}

void setup()
{
  Serial.begin(115200);
  pinMode(PB14,OUTPUT);

  // can.filterMask16Init(0, 0x05, 0x7ff); // rx filter bank 0 definition. only allow ID == 5 into fifo0
  can.filterList16Init(2, 5, 10, 21, 0x3f0); // rx bank 2. passes IDs == 5, 10, 21 and 0x3f0

  Serial.println(VERS);
}

void loop()
{
  digitalWrite(PB14, HIGH);
  delay(500);
  digitalWrite(PB14, LOW);
  delay(500);
  Serial.println(VERS);
  return;
  // id = canRX(PRINT); // check/get a msg
  id = canRX(); // check/get a msg
  if (id == 0x70c)
  {
    int i = rxMsg.bytes[0];
    int rcs = rxMsg.bytes[1]*0.5 - 50;
    int range = (rxMsg.bytes[2] * 256 + rxMsg.bytes[3]);
    int angle = rxMsg.bytes[4] - 90;
    double vel = (rxMsg.bytes[5] * 256 + rxMsg.bytes[6]) * 0.05 - 35;
    int roll = rxMsg.bytes[5];
    sprintf(cBuff, "{\"id\":%d,\"rcs\":%d,\"range\":%d,\"angle\":%d,\"vel\":%d,\"roll\":%d}", i, rcs, range, angle, int(vel*100), roll);
    Serial.println(cBuff);
    // Serial.println(range);
    // Serial.println(vel);
  }
  return;

}
//  */
